package com.example.administrator.questionanswer;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.administrator.questionanswer.adapter.DataComplileAdapter;
import com.example.administrator.questionanswer.application.MyApplication;
import com.example.administrator.questionanswer.dao.AnswerBeanDao;
import com.example.administrator.questionanswer.dao.DaoSession;
import com.example.administrator.questionanswer.greendao.AnswerBean;
import com.example.administrator.questionanswer.greendao.PercentBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/6 0006.
 */

public class DataCompileActivity extends Activity {
    @BindView(R.id.recycle)
    RecyclerView recycle;
    List<PercentBean> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_complie);
        ButterKnife.bind(this);
        DaoSession daoSession = MyApplication.getInstances().getDaoSession();
        AnswerBeanDao answerBeanDao = daoSession.getAnswerBeanDao();
        List<AnswerBean> answerBeans = answerBeanDao.loadAll();
        list = new ArrayList<>();


        for (int i = 0; i < answerBeans.size(); i++) {

            AnswerBean answerBean = answerBeans.get(i);
            String questionContent = answerBean.getQuestionContent();
            String major = answerBean.getMajor();
            String questionAContent = answerBean.getQuestionAContent();
            String questionBContent = answerBean.getQuestionBContent();
            String questionCContent = answerBean.getQuestionCContent();
            String questionDContent = answerBean.getQuestionDContent();
            String questionEContent = answerBean.getQuestionEContent();
            String questionFContent = answerBean.getQuestionFContent();
            String userName = answerBean.getUserName();
            String questionName = answerBean.getQuestionName();
            Long questionId = answerBean.getQuestionId();

            PercentBean percentBean = new PercentBean();
            percentBean.setUserName(userName);
            percentBean.setQuestionName(questionName);
            percentBean.setQuestionId(questionId);
            percentBean.setMajor(major);
            percentBean.setQuestionAContent(questionAContent);
            percentBean.setQuestionBContent(questionBContent);
            percentBean.setQuestionCContent(questionCContent);
            percentBean.setQuestionDContent(questionDContent);
            percentBean.setQuestionEContent(questionEContent);
            percentBean.setQuestionFContent(questionFContent);


            if (questionId != 6) {

                long a = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("A"))
                        .count();

                long b = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("B"))
                        .count();

                long c = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("C"))
                        .count();

                long d = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("D"))
                        .count();

                long e = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("E"))
                        .count();

                long f = answerBeanDao.queryBuilder()
                        .where(AnswerBeanDao.Properties.QuestionId.eq(answerBean.getQuestionId()), AnswerBeanDao.Properties.QuestionContent.eq("F"))
                        .count();

                long total = a + b + c + d + f;
                String aPercent = formart(a * 1.0 / total * 100);
                String bPercent = formart(b * 1.0 / total * 100);
                String cPercent = formart(c * 1.0 / total * 100);
                String dPercent = formart(d * 1.0 / total * 100);
                String ePercent = formart(e * 1.0 / total * 100);
                String fPercent = formart(f * 1.0 / total * 100);
                percentBean.setQuestionAPercent(aPercent + "%");
                percentBean.setQuestionBPercent(bPercent + "%");
                percentBean.setQuestionCPercent(cPercent + "%");
                percentBean.setQuestionDPercent(dPercent + "%");
                percentBean.setQuestionEPercent(ePercent + "%");
                percentBean.setQuestionFPercent(fPercent + "%");

                list.add(percentBean);
            } else {
                
            }

        }

        DataComplileAdapter dataComplileAdapter = new DataComplileAdapter(this);
        dataComplileAdapter.setList(list);
        recycle.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycle.setAdapter(dataComplileAdapter);
        dataComplileAdapter.notifyDataSetChanged();
    }

    public String formart(double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        String format = decimalFormat.format(number);
        return format;
    }
}
